package com.leo.tool.jdk.jdk8;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.junit.Test;

import java.util.Comparator;
import java.util.stream.Stream;

import static java.util.Comparator.*;

public class ComparatorTest {

    @Test
    public void testNullFirst() {
        Comparator<User> comparator = nullsLast(comparing(User::getNum, nullsLast(Integer::compare)))
                .thenComparing(User::getAge, nullsFirst(Integer::compare))
                .thenComparing(
                        User::getAccount,
                        nullsLast(comparing(Account::getBalance, nullsLast(Long::compare)))
                );

        User user1 = User.builder().build();
        User user2 = User.builder().account(new Account(1L)).build();
        User user3 = User.builder().account(new Account(2L)).build();
        User user4 = User.builder().num(1).build();
        Stream.of(null, user1, user2, user3, user4).sorted(comparator).forEach(System.out::println);
    }

    @Builder
    @Data
    static class User {
        Integer num;
        Integer age;
        Account account;
    }

    @AllArgsConstructor
    @Data
    static class Account {
        Long balance;
    }
}
